//
//  WPVideoDetailVC.m
//  wobingwoyi
//
//  Created by WGP on 16/9/2.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPVideoDetailVC.h"
#import "ZXVideoPlayerController.h"
#import "ZXVideo.h"
#import "WPCustomActionSheet.h"

@interface WPVideoDetailVC ()<WPCustomActionSheetDelegate>

@property (nonatomic, strong) ZXVideoPlayerController *videoController;
@property (nonatomic, strong)UIView *containtView;
@property (nonatomic, strong) ZXVideo *video;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIImageView *playImageView;
@property(nonatomic,strong)UILabel *playCountLabel;
@property(nonatomic,strong)UITextView *textView;
@property(nonatomic,strong)UILabel *praiseLabelCount;
@property(nonatomic,strong)UIButton *praiseBtn;
@property(nonatomic,strong)UIButton *shareBtn;
@property(nonatomic,assign)BOOL isShow;
@property(nonatomic,strong)NSArray *sharedTypeArr;

@end

@implementation WPVideoDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = GlobalBgColor;
    
    [self containtView];
    [self setupUI];
    
    [self playVideo];
}

-(void)setupUI
{
    [self praiseBtn];
    [self praiseLabelCount];
    [self shareBtn];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)playVideo
{
    if (!self.videoController)
    {
        self.videoController = [[ZXVideoPlayerController alloc] initWithFrame:CGRectMake(0, 0, kZXVideoPlayerOriginalWidth, kZXVideoPlayerOriginalHeight)];
        
        __weak typeof(self) weakSelf = self;
        self.videoController.videoPlayerGoBackBlock = ^{
            __strong typeof(self) strongSelf = weakSelf;
            
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
            
            [strongSelf.navigationController popViewControllerAnimated:YES];
            [strongSelf.navigationController setNavigationBarHidden:NO animated:YES];
            
            [[NSUserDefaults standardUserDefaults] setObject:@0 forKey:@"ZXVideoPlayer_DidLockScreen"];
            
            strongSelf.videoController = nil;
        };
        
        self.videoController.videoPlayerWillChangeToOriginalScreenModeBlock = ^()
        {
        };
        self.videoController.videoPlayerWillChangeToFullScreenModeBlock = ^()
        {
        };
        
        [self.videoController showInView:self.view];
    }
    
    self.videoController.video = self.video;
}

-(UIView *)containtView
{
    if (_containtView == nil)
    {
        _containtView = [[UIView alloc] initWithFrame:CGRectMake(0, kZXVideoPlayerOriginalHeight+10, SCREEN_WIDTH, 169)];
        [self.view addSubview:_containtView];
        _containtView.backgroundColor = [UIColor whiteColor];
        
        UIImageView *firstLine = [self lineImageViewWith:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
        UIImageView *secondLine = [self lineImageViewWith:CGRectMake(15, 44, SCREEN_WIDTH-15, 0.5)];
        UIImageView *thirdLine = [self lineImageViewWith:CGRectMake(0, 169, SCREEN_WIDTH, 0.5)];
        
        [_containtView addSubview:firstLine];
        [_containtView addSubview:secondLine];
        [_containtView addSubview:thirdLine];
        
        self.titleLabel = [UILabel new];
        self.titleLabel.font = [UIFont systemFontOfSize:15];
        self.titleLabel.text = self.model.title;
        self.titleLabel.textColor = RGB_SAME(51);
        [self.titleLabel sizeToFit];
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
//        self.titleLabel.backgroundColor = [UIColor redColor];
        [_containtView addSubview:self.titleLabel];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_containtView).offset(15);
            make.top.equalTo(_containtView).offset(1);
            make.height.mas_equalTo(43);
        }];
        
        self.playCountLabel = [UILabel new];
        [_containtView addSubview:self.playCountLabel];
        self.playCountLabel.font = [UIFont systemFontOfSize:11];
        self.playCountLabel.textColor = RGB_SAME(51);
        self.playCountLabel.textAlignment = NSTextAlignmentLeft;
        self.playCountLabel.text = [NSString stringWithFormat:@"%zd",self.model.readAmount];
        [self.playCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_containtView).offset(-10);
            make.centerY.height.equalTo(self.titleLabel);
            make.width.mas_equalTo(32);
        }];
        
        self.playImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bofang_count"]];
        [self.playImageView sizeToFit];
        [_containtView addSubview:self.playImageView];
        
        [self.playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.titleLabel);
            make.right.equalTo(self.playCountLabel.mas_left).offset(-5);
        }];
        
        self.textView = [UITextView new];
        self.textView.editable = NO;
        [_containtView addSubview:self.textView];
        self.textView.font = [UIFont systemFontOfSize:13];
        self.textView.textColor = RGB_SAME(102);
        self.textView.text = self.model.descp;
        
        [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_containtView).offset(10);
            make.right.equalTo(_containtView).offset(-10);
            make.bottom.equalTo(_containtView).offset(-1);
            make.top.equalTo(secondLine.mas_bottom).offset(10);
        }];
        
    }
    return _containtView;
}

-(UIButton *)praiseBtn
{
    if (_praiseBtn == nil)
    {
        _praiseBtn = [WPBtnNoHighLight buttonWithType:UIButtonTypeCustom];
        [_praiseBtn setBackgroundImage:[UIImage imageNamed:@"video_praise_normal"] forState:UIControlStateNormal];
        [_praiseBtn setBackgroundImage:[UIImage imageNamed:@"video_praise"] forState:UIControlStateSelected];
        [_praiseBtn sizeToFit];
        [_praiseBtn addTarget:self action:@selector(praiseBtnDidClick:) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:_praiseBtn];
        
        [_praiseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.containtView.mas_bottom).offset(10);
            make.right.equalTo(self.view).offset(-50);
        }];
    }
    return _praiseBtn;
}

-(UILabel *)praiseLabelCount
{
    if (_praiseLabelCount == nil)
    {
        _praiseLabelCount = [UILabel new];
        _praiseLabelCount.font = [UIFont systemFontOfSize:13];
        _praiseLabelCount.textColor = RGB_SAME(102);
        _praiseLabelCount.text = [NSString stringWithFormat:@"%zd",self.model.praiseAmount];
        _praiseLabelCount.textAlignment = NSTextAlignmentLeft;
        [_praiseLabelCount sizeToFit];
        [self.view addSubview:_praiseLabelCount];
        
        [_praiseLabelCount mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.praiseBtn.mas_right).offset(8);
            make.bottom.equalTo(self.praiseBtn);
            
        }];
    }
    return _praiseLabelCount;
}

-(UIButton *)shareBtn
{
    if (_shareBtn == nil)
    {
        _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shareBtn setBackgroundImage:[UIImage imageNamed:@"videoSharedImage"] forState:UIControlStateNormal];
        [_shareBtn addTarget:self action:@selector(shareBtnDidClick:) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:_shareBtn];
        
        [_shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.praiseBtn.mas_left).offset(-25);
            make.centerY.equalTo(self.praiseBtn);
            make.width.mas_equalTo(18);
        }];
    }
    return _shareBtn;
}

-(void)praiseBtnDidClick:(UIButton *)btn
{
    if(btn.selected)
    {
        //        弹穿提示已经赞过
        [self showMessageLabelWith:@"亲, 已经赞过了~" font:[UIFont systemFontOfSize:13]];
        return ;
    }
    
    self.model.readAmount += 1;
    self.playCountLabel.text = [NSString stringWithFormat:@"%zd",self.model.readAmount];
    
    NSString *url = @"video/praise.do";
    NSDictionary *dict = @{@"id":@(self.model.id)};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    self.praiseBtn.selected = !btn.selected;
}

-(void)showMessageLabelWith:(NSString *)title font:(UIFont *)font
{
    if (self.isShow)
    {
        return ;
    }
    UIButton *messageBtn= [UIButton buttonWithType:UIButtonTypeCustom];
    [messageBtn setTitle:title forState:UIControlStateNormal];
    messageBtn.titleLabel.font = font;
    messageBtn.backgroundColor = RGBA_COLOR(51, 51, 51, 0.6);
    messageBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    [messageBtn sizeToFit];
    messageBtn.layer.cornerRadius = 3;
    messageBtn.layer.masksToBounds = YES;
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:messageBtn];
    self.isShow = YES;
    messageBtn.center = CGPointMake(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.6);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [UIView animateWithDuration:1 animations:^{
            
            messageBtn.alpha = 0;
            
        } completion:^(BOOL finished) {
            
            [messageBtn removeFromSuperview];
            self.isShow = NO;
        }];
        
    });
    
}



-(void)shareBtnDidClick:(UIButton *)btn
{
    [self.videoController pauseButtonClick];
    
    WPCustomActionSheet *cusSheet = [[WPCustomActionSheet alloc] init];
    cusSheet.delegate = self;
    NSArray *contentArray = @[@{@"name":@"微信",@"icon":@"wechat"},
                              @{@"name":@"朋友圈 ",@"icon":@"FriendsCircle"},
                              @{@"name":@"QQ ",@"icon":@"iconfont-qq"},
                              @{@"name":@"QQ空间",@"icon":@"iconfont-qzone"},
                              @{@"name":@"短信",@"icon":@"duanxin"},
                              @{@"name":@"微博",@"icon":@"yaoqin_weibo"},
                              @{@"name":@"邮件",@"icon":@"email"}
                              ];
    
    [cusSheet showInView:[UIApplication sharedApplication].keyWindow contentArray:contentArray];
}

#pragma mark - YXCustomActionSheetDelegate

- (void) customActionSheetButtonClick:(UIButton *)btn
{
    
    // share type
    NSUInteger typeUI = 0;
    switch ((long)btn.tag) {
        case 0:
        {
            typeUI = [self.sharedTypeArr[0] unsignedIntegerValue];
        }
            break;
            
        case 1:
        {
            typeUI = [self.sharedTypeArr[1] unsignedIntegerValue];
        }
            break;
            
        case 2:
        {
            typeUI = [self.sharedTypeArr[2] unsignedIntegerValue];
        }
            break;
            
        case 3:
        {
            typeUI = [self.sharedTypeArr[3] unsignedIntegerValue];
        }
            break;
            
        case 4:
        {
            typeUI = [self.sharedTypeArr[4] unsignedIntegerValue];
        }
            break;
            
        case 5:
        {
            typeUI = [self.sharedTypeArr[5] unsignedIntegerValue];
        }
            break;
            
        case 6:
        {
            typeUI = [self.sharedTypeArr[6] unsignedIntegerValue];
        }
            break;
            
        case 7:
        {
            typeUI = [self.sharedTypeArr[7] unsignedIntegerValue];
        }
            break;
            
        default:
            break;
    }
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:self.model.descp
                                     images:self.videoImage //传入要分享的图片
                                        url:[NSURL URLWithString:self.model.videoUrl]
                                      title:self.model.title
                                       type:SSDKContentTypeAuto];
    
    [ShareSDK share:typeUI
         parameters:shareParams
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
         
         
         if (error)
         {
             if (error.code == 208)
             {
                 NSString *platform = nil;
                 if (btn.tag == 0 || btn.tag == 1 || btn.tag == 4)
                 {
                     platform = @"微信";
                 }
                 else if(btn.tag == 2 || btn.tag == 3)
                 {
                     platform = @"QQ";
                 }
                 
                 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"您的设备没有安装%@",platform] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                 [alertView show];
             }
         }
         else
         {
//             WPLog(@"%@",userData);
         }
         
     }];
}



-(NSArray *)sharedTypeArr
{
    if (_sharedTypeArr == nil)
    {
        _sharedTypeArr = @[@(SSDKPlatformTypeWechat),
                           @(SSDKPlatformSubTypeWechatTimeline),
                           @(SSDKPlatformSubTypeQQFriend),
                           @(SSDKPlatformSubTypeQZone),
                           @(SSDKPlatformTypeSMS),
                           @(SSDKPlatformTypeSinaWeibo),
                           @(SSDKPlatformTypeMail),
                           ];
    }
    return _sharedTypeArr;
}


-(UIImageView *)lineImageViewWith:(CGRect)rect
{
    UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:rect];
    lineImageView.backgroundColor = GLobalLineColor;
    
    return lineImageView;
}

-(void)setModel:(WPVideoModel *)model
{
    _model = model;
    self.video = [[ZXVideo alloc] init];
    self.video.playUrl = self.model.videoUrl;
//    self.video.playUrl = @"http://ob9qh2yi0.bkt.clouddn.com/IOS%E4%BC%81%E4%B8%9A%E7%89%88%E8%AE%BE%E7%BD%AE%E6%B5%81%E7%A8%8B.mp4";
//    self.video.playUrl = @"http://player.youku.com/embed/XNTA5NTkzNTM2";
    //@"http://player.youku.com/embed/XNTA5NTkzNTM2"
}

@end
